<?php
class PageController extends Zendme_Controller_Action
{
	public function init(){
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] 
									 . '/' . $this->_arrParam['controller'];
		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index';	
							 
		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		
		if(empty($ssFilter->col)){
			$ssFilter->lang 		= '';
		}
		$this->_arrParam['ssFilter']['lang'] 		= $this->_arrParam['lang'];	
		
		$info = new Zendme_System_Info();
		$language = $info->getLanguage('admin');
		if(empty($language)){
			$language = 'vi';
		}
		$translate = array(
							'adapter' => 'tmx',
							'content' => LANG_PATH . '/' . $language . '/admin/menu.tmx',
							'locale' => $language,
							);
		$translate = new Zend_Translate($translate);
		Zend_Registry::set('Zend_Translate', $translate);
		
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
		
		$template_path = TEMPLATE_PATH . "/admin/system";
		$this->loadTemplate($template_path,'template.ini','template');	
	}
	
	public function indexAction(){
		$this->view->Title = $this->view->translate('Admin » Pages » List');
		$this->view->headTitle($this->view->Title,true);		
		
		$mdlPages = new Zendme_Models_Pages();
		$this->view->items = $mdlPages->listItem($this->_arrParam, array('task'=>'admin-list'));

		$totalItem=$mdlPages->countItem($this->_arrParam);	
		$paginator = new Zendme_Paginator();
		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
	}
	
	public function addAction(){
		$this->view->Title = $this->view->translate('Admin » Pages » Add New');
		$this->view->headTitle($this->view->Title,true);
		$tblSlide = new Zendme_Models_Pages();		
		if($this->_request->isPost()){
			$validator = new Default_Form_ValidateSlide($this->_arrParam);
			if($validator->isError() == true){
				$this->view->errors = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
			}else{
				$arrParam = $validator->getData(array('upload'=>true));
				$tblSlide	->saveItem($arrParam,array('task'=>'admin-add'));
				$this->_redirect($this->_actionMain);
			}
		}
		$this->render('edit');
	}
	
	public function editAction(){
		$this->view->Title = 'Pages :: Edit';
		$this->view->headTitle($this->view->Title,true);
		
		$mdlService=new Zendme_Models_Pages();
		$item=$mdlService->getItem($this->_arrParam,array('task'=>'admin-edit'));
		$this->view->item=$item;
		
		if($this->_request->isPost()){
			$validator = new Default_Form_ValidatePage($this->_arrParam);
			if($validator->isError() == true){
				$this->view->errors = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
			}else
			{
				$arrParam = $validator->getData(array('upload'=>true));
				$mdlService	->saveItem($arrParam,array('task'=>'admin-edit'));
				$this->_redirect($this->_arrParam['module'].'/'.$this->_arrParam['controller'].'/'
								.$this->_arrParam['action'].'/id/'.$this->_arrParam['id']);
			}
		}
	}
	
	public function statusAction(){
		$mdlService = new Zendme_Models_Pages();
		$mdlService->changeStatus($this->_arrParam,array('task'=>'status'));
		//$this->_redirect($this->_actionMain);		
		//$this->_helper->viewRenderer->setNoRender();
		die();
	}

	public function featureAction(){
		$mdlService = new Zendme_Models_Pages();
		$mdlService->changeFeatured($this->_arrParam,array('task'=>'featured'));
		//$this->_redirect($this->_actionMain);		
		//$this->_helper->viewRenderer->setNoRender();
	}
	
	public function multiDeleteAction(){		
//		if($this->_request->isPost()){
//			$tblProduct = new Zendme_Models_Products();
//			$tblProduct->deleteItem($this->_arrParam,array('task'=>'admin-multi-delete'));
//			$this->_redirect($this->_actionMain);
//		}
//		$this->_helper->viewRenderer->setNoRender();
		
		$this->_redirect($this->_actionMain);
	}
}
